package com.ppt.convert.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ppt.convert.domain.Result;
import com.ppt.convert.util.HttpClientUtil;
import com.ppt.convert.util.SignCrypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author zcx
 * @Title 上传文件到网盘
 * @date 2020年01月20日 14:37
 **/
@Service
public class UploadService {

    @Value("${vedio.app.key}")
    private String vedioAppKey;

    @Value("${vedio.appScret}")
    private String vedioAppSecret;

    @Value("${vedio.upload.url}")
    private String vedioUploadUrl;

    public String upload(MultipartFile file) {
        TreeMap<String,String> params = new TreeMap<>();
        String timeStamp = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        params.put("timestamp", timeStamp);
        params.put("isOpen","T");
        params.put("appKey", vedioAppKey);
        String sign = SignCrypt.createSign(params,vedioAppSecret);
        Map<String,String> map = new HashMap<>();
        map.put("timestamp",timeStamp);
        map.put("isOpen","T");
        map.put("appKey", vedioAppKey);
        map.put("sign", sign);
        String resp = "";
        try {
            resp = HttpClientUtil.httpClientUploadFile(vedioUploadUrl, file, map);
        }catch (Exception e){
            return "error";
        }
        if(StringUtils.isBlank(resp)){
            return "error";
        }
        return resp;
    }
}
